


const baseUrl = "http://sys.wxiaoguan.com"
const ApiUrl = baseUrl + "/index.php/api"
const sendRequest = function(param, backpage, backtype) {
	var _self = this,
		url = param.url,
		method = param.method,
		header = {},
		data = param.data || {},
		hideLoading = param.hideLoading || false;

	//拼接完整请求地址
	var requestUrl = ApiUrl + url;
	//请求方式:GET或POST(POST需配置header: {'content-type' : "application/x-www-form-urlencoded"},)
	if (method) {
		method = method.toUpperCase(); //小写改为大写
		if (method == "POST") {
			header = {
				'content-type': "application/x-www-form-urlencoded"
			};
		} else {
			
		}
	} else {
		method = "GET";
	}
	let token = ""
	if (uni.getStorageSync('userInfo')) {
		token = JSON.parse(uni.getStorageSync('userInfo')).token
	}
	if (token) {
		header.Authorization = token
	}
	//用户交互:加载圈
	if (!hideLoading) {
		uni.showLoading({
			title: '加载中...'
		});
	}

	//网络请求
	uni.request({
		url: requestUrl,
		method: method,
		header: header,
		data: data,
		success: res => {
			if (res.statusCode && res.statusCode != 200) { //api错误
				uni.showModal({
					content: "" + res.errMsg
				});
				return;
			}
			if (res.data.code) { //返回结果码code判断:0成功,1错误,-1未登录(未绑定/失效/被解绑)
				if (res.data.code == "-1") {
					_self.login(backpage, backtype);
					return;
				}
				if (res.data.code != "0") {
					uni.showModal({
						showCancel: false,
						content: "" + res.data.msg
					});
					return;
				}
			} else {
				// uni.showModal({
				// 	showCancel: false,
				// 	content: "No ResultCode:" + res.data.msg
				// });
				return;
			}
			typeof param.success == "function" && param.success(res.data);
		},
		fail: (e) => {
			// uni.showModal({
			// 	content: "" + e.errMsg
			// });
			typeof param.fail == "function" && param.fail(e.data);
		},
		complete: () => {
			if (!hideLoading) {
				uni.hideLoading();
			}
			typeof param.complete == "function" && param.complete();
			return;
		}
	});
}


export {
	sendRequest,
	ApiUrl
}
